package com.liwenfeng.diancanspringboot.controller.admin;

import com.liwenfeng.diancanspringboot.common.result.PageResult;
import com.liwenfeng.diancanspringboot.common.result.Result;
import com.liwenfeng.diancanspringboot.pojo.dto.DishDTO;
import com.liwenfeng.diancanspringboot.pojo.dto.DishPageQueryDTO;
import com.liwenfeng.diancanspringboot.pojo.entity.Dish;
import com.liwenfeng.diancanspringboot.pojo.entity.DishFlavor;
import com.liwenfeng.diancanspringboot.pojo.vo.DishVO;
import com.liwenfeng.diancanspringboot.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理")
public class DishController {
    @Autowired
    private DishService dishService;
    @PutMapping
    @ApiOperation("修改菜品")
    public Result<String> update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品:{}",dishDTO);
        dishService.update(dishDTO);
        return Result.success();
    }

    // 批量删除菜品
    // 参数说明：前端传过来的是String :1,2,3,4,5
    // 可以用字符串接收，再用split方法分割处理，
    // 也可以用List<Long>接收，需要添加注解@RequestParam
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result<String> delete(@RequestParam List<Long> ids){
//        String[] split = ids.split(",");
//        List<Long> list = Arrays.stream(split).map(Long::parseLong).toList();

        log.info("删除菜品:{}", ids);

        dishService.delete(ids);
        return Result.success();
    }

    @PostMapping
    @ApiOperation("新增菜品")
    public Result<String> save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品:{}",dishDTO);
        dishService.save(dishDTO);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据id查询菜品:{}",id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<DishVO>> list(long categoryId){
        log.info("查询菜品列表: {}", categoryId);
        List<DishVO> dishVOs = dishService.listByCategoryId(categoryId);
        return Result.success(dishVOs);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询: {}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("启用或禁用菜品")
    public Result<String> startOrStop(@PathVariable Integer status, Long id){
        log.info("启用或禁用菜品: {}, {}", status, id);
        dishService.startOrStop(status, id);
        return Result.success();
    }

    @GetMapping("/flavor")
    @ApiOperation("根据菜品id查询菜品口味")
    public Result<DishVO> list(Long dishId){
        log.info("根据菜品id查询菜品口味: {}", dishId);
        DishVO dishVO = dishService.getByIdWithFlavor(dishId);
        return Result.success(dishVO);
    }
}
